package de.hauck.rtmidlet.views;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

import de.hauck.rtmidlet.RTMidlet;
import de.hauck.rtmidlet.model.Configuration;

public class CanvasDummy extends Canvas implements CommandListener {
	private Font f;
	private Displayable _dble;

	public CanvasDummy(Displayable dble) {
		f = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN,
				Font.SIZE_SMALL);
		_dble = dble;
	}

	protected void paint(Graphics g) {
		g.setColor(0, 0, 0);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setFont(f);
		System.out.println("downloading...");
		String url = "http://mobile.finanztreff.de";
		String sb = openUrl(url, false);
		System.out.println(sb);
		int index = sb.indexOf("Mein Finanztreff");
		System.out.println("index: " + index);
		if (index != -1) {
			int start = sb.indexOf("/sevenval.fit", index - 170);
			
			int ende = sb.indexOf("http://www.finanztreff.de", index-170);
			String new_request = sb.substring(start, ende);
			System.out.println(new_request+"http://www.finanztreff.de/ftreff2/anmeldung.htn");
			String result = openUrl(url + new_request+"http://www.finanztreff.de/ftreff2/anmeldung.htn", true);
			System.out.println(result);

		}

	}

	public void commandAction(Command command, Displayable dble) {

	}

	protected void keyPressed(int keyCode) {
		int action = getGameAction(keyCode);
		if (keyCode == KEY_NUM0) {
			RTMidlet.getInstance().show(_dble);
		}
	}

	private String openUrl(String url, boolean logon) {
		InputStream is = null;
		String sb = "";

		try {
			HttpConnection hc = (HttpConnection) Connector.open(url);
			if (hc != null) {
				if (logon) {
					hc.setRequestMethod(HttpConnection.POST);
					hc.setRequestProperty("sektion", "login");
					hc.setRequestProperty("u", "100");
					hc
							.setRequestProperty("url",
									"http://portfolio.finanztreff.de/depot_uebersicht.htn");
					hc.setRequestProperty("lg", "");
					hc.setRequestProperty("pw", "");
					hc.setRequestProperty("sv_submit", "Go");
				}
				is = hc.openInputStream();
				// _yahooCanvas.updateStatus(c.CONN_STATUS_CONNECTING);
				int ch; // Check the Content-Length first
				long length = hc.getLength();
				Configuration.addToDownloadCounter(length);

				if (length != -1) {
					// _yahooCanvas.updateStatus(c.CONN_STATUS_CONNECTED);
					for (int i = 0; i < length; i++)
						if ((ch = is.read()) != -1)
							sb += ((char) ch);
				} else {
					// if the content-length is not available
					while ((ch = is.read()) != -1)
						sb += ((char) ch);

				}
				is.close();
				hc.close();

			}
			return sb;
		} catch (IOException e) {
			e.printStackTrace();
			return sb;
			// _main.alert("Error", e.getMessage(), c.alertWaitTime);
		} catch (Exception e) {
			e.printStackTrace();
			return sb;
			// _main.alert("Error", e.getMessage(), c.alertWaitTime);
		}

	}
}
